package cn.itcast.service;

import cn.itcast.dao.ConsumerDao;
import cn.itcast.dao.EmpFeignClient;
import cn.itcast.pojo.Emp;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class ConsumerService {

   /* @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    public List<Emp> queryUserById(List<Integer> ids){
        List<Emp> empList = new ArrayList<>();
        //直接写服务名称即可
        String url = "http://user-service/emp/";
        for (Integer id : ids) {
            empList.add(restTemplate.getForObject(url+id,Emp.class));
        }
        return empList;
    }*/
    /*public List<Emp> queryUserById(List<Integer> ids){
        List<Emp> empList = new ArrayList<>();
        //根据服务名称,获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        ServiceInstance instance = instances.get(0);
        //获取ip和端口信息
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/emp/";
        for (Integer id : ids) {
            empList.add(restTemplate.getForObject(url+id,Emp.class));
        }
        return empList;
    }*/

   /* @Autowired
    private ConsumerDao consumerDao;*/

   @Autowired
   private EmpFeignClient empFeignClient;

    public List<Emp> queryUserById(List<Integer> ids) {
        List<Emp> empList = new ArrayList<>();
        for (Integer id : ids) {
            Emp emp = empFeignClient.queryUserById(id);
            empList.add(emp);
        }
        return empList;
    }
}
